package com.app.library.base

import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.app.library.R
import com.app.library.ext.viewbinding.base.ActivityBinding
import com.app.library.ext.viewbinding.base.ActivityBindingDelegate
import com.app.library.utils.CustomToast
import com.app.library.utils.ToasterUtils
import com.app.library.utils.ViewModelUtils
import com.blankj.utilcode.util.KeyboardUtils
import retrofit2.HttpException

abstract class LibActivity<VB : ViewBinding, VM : LibViewModel> : AppCompatActivity(), LibView,
    ActivityBinding<VB> by ActivityBindingDelegate() {

    protected val tagKey: String = javaClass.simpleName

    private var loadingDialog: LoadingDialog? = null
    lateinit var mViewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE)//去掉标题栏
        beforeSetContentView()
        setContentViewWithBinding()
        initBaseModel()
        initData(savedInstanceState)
        initView()
    }

    private fun initBaseModel() {
        mViewModel = ViewModelUtils.getWithGeneric(this)
        mViewModel.apply {
            toast.observe(this@LibActivity) {
                showToast(it)
            }
            loading.observe(this@LibActivity) {
                if (it == null) {
                    dismissLoading()
                } else {
                    showLoading(it)
                }
            }
            exception.observe(this@LibActivity) {
                handleError(it)
            }
            initViewModel(this)
        }
    }

    open fun initViewModel(vm: VM) {

    }


   open fun handleError(throwable: Throwable) {
        throwable.printStackTrace()
        when (throwable) {
            is ApiException -> {
                handleApiExp(throwable)
            }
            is HttpException -> {
                ToasterUtils.showShort(getString(R.string.error_no_network))
            }
            else -> {
                ToasterUtils.showShort(getString(R.string.error_no_network))
            }
        }
    }

    abstract fun handleApiExp(exception: ApiException)

    open fun beforeSetContentView() {}

    override fun showLoading(msg: String?) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog(this)
            loadingDialog?.setCanceledOnTouchOutside(false)
            loadingDialog?.setCancelable(true)
        }
        if (loadingDialog?.isShowing == false)
            loadingDialog?.show(msg)

    }

    override fun dismissLoading() {
        loadingDialog?.dismiss()
    }

    override fun showToast(msg: String?) {
        msg?:return
        ToasterUtils.showShort(msg)
    }

    override fun showToast(res: Int) {
        showToast(getString(res))
    }

    override fun onDestroy() {
        RxNetLife.clear(tagKey)
        KeyboardUtils.hideSoftInput(this)
        super.onDestroy()
    }

}